//app.js
App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },
  getRoomNo:function(){
    return this.globalData.roomNo;
  },
  getLoginUser:function(){
    return this.globalData.loginUser;
  },
  setRoomNo:function(roomNo){
    this.globalData.roomNo = roomNo;
  },
  getUserInfo:function(cb){
    var loginUrl = "http://localhost:8888/user/login";
    var that = this
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      //调用登录接口
      wx.login({
        success: function (res) {
          if(res.errMsg!="login:ok"){
            return ;
          }
          var code = res.code;
          wx.getUserInfo({
            success: function (res) {
             console.log(JSON.stringify(res))
              var user = res.userInfo;
              that.globalData.userInfo = res.userInfo
             
              console.log("调用微信登录。。。。。。")
              wx.request({
                url: loginUrl,
                data: {
                  jscode:code,
                  nickName:user.nickName,
                  avatarUrl:user.avatarUrl
                },
                method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
                // header: {}, // 设置请求的 header
                success: function(res){
                  
                  var data = res.data.result;
                  data["nickName"] = user.nickName;
                  console.log("login :"+JSON.stringify(data));
                  that.globalData.loginUser = data;
                  console.log("login :"+JSON.stringify( that.globalData.loginUser));
                   typeof cb == "function" && cb(that.globalData.userInfo);
                },
                fail: function() {
                  // fail
                },
                complete: function() {
                  // complete
                }
              })
            }
          })
        }
      })
    }
  },
  globalData:{
    userInfo:null,
    roomNo:null,
    loginUser:null
  }
})